﻿CREATE FUNCTION dbo.FormatPhone
(@Phone varchar(50))  
RETURNS varchar(15) AS  
BEGIN 

DECLARE @Ret varchar(50)

SET @Ret = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@Phone,'-',''),'(',''),')',''),' ',''),'/','')

IF LEN(@Ret) = 10
	RETURN SUBSTRING(@Ret,1,3) + '-'+ SUBSTRING(@Ret,4,3) + '-' + SUBSTRING(@Ret,7,4)

IF LEN(@Ret) = 11
	RETURN '1'+'-'+SUBSTRING(@Ret,2,3) + '-'+ SUBSTRING(@Ret,5,3) + '-' + SUBSTRING(@Ret,8,4)

IF LEN(@Ret) = 7
	RETURN SUBSTRING(@Ret,1,3) + '-' + SUBSTRING(@Ret,4,4)

RETURN @Ret

END







